package com.cenwangkeji.resident.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.ServicePackageBean;
import com.cenwangkeji.resident.model.bean.User;
import com.cenwangkeji.resident.ui.activity.ServicePackageDetailActivity;
import com.cenwangkeji.resident.ui.adapter.ServicePackageFragmentAdpter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lws.basic.base.BaseFragment;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

/**
 * @松 on 2019/10/30 09:09
 * 服务包
 */
public class ServicePackageFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.rv)
    RecyclerView mRv;
    Unbinder unbinder;
    private ServicePackageFragmentAdpter mAdpter;

    @Override
    public int setLayoutId() {
        return R.layout.fragment_service_package;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        unbinder = ButterKnife.bind(this, rootView);
    }

    @Override
    protected void initData() {
        super.initData();
        queryService();//获取服务包
        initRecyclerView();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    //查询健康服务包
    public void queryService(){
        showLoading();
        Observable.create(new ObservableOnSubscribe<ServicePackageBean>() {
            @Override
            public void subscribe(ObservableEmitter<ServicePackageBean> emitter) throws Exception {
                Map<Object,Object> map = new HashMap<>();
                map.put("mark",3);
                map.put(Constant.token, User.getToken());
                map.put("userId", User.getUserId());
                JSONObject jsonObject = new JSONObject(map);
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.service_package_user);
                MyLogger.i(json);
                Gson gson = new Gson();
                emitter.onNext(gson.fromJson(json, ServicePackageBean.class ));
            }
        }).compose(RxUtils.<ServicePackageBean>io_main())
                .subscribe(new ObserverOnNext<ServicePackageBean>() {


                    @Override
                    public void onNext(ServicePackageBean bean) {
                          if (bean.getData() !=null&& bean.getData().size()>0){
                              mAdpter.setNewData(bean.getData());
                          }else {
                              MyToast.show(bean.getMsg());
                              View emptyView=getLayoutInflater().inflate(R.layout.view_empty, null);
                              mAdpter.setEmptyView(emptyView);
                          }
                          dismissLoading();
                    }
                });

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        ServicePackageBean.DataBean bean = mAdpter.getData().get(position);
        if (bean.getType()==0){
            MyToast.show("未制定");
        }else {
            Intent intent = new Intent(getActivity(), ServicePackageDetailActivity.class);
            intent.putExtra("date", bean);
            startActivity(intent);
        }
    }

    private void initRecyclerView(){
        GridLayoutManager manager = new GridLayoutManager(getContext(), 2);
        mRv.setLayoutManager(manager);
        mAdpter = new ServicePackageFragmentAdpter(null);
        mAdpter.setOnItemClickListener(this);
        mRv.setAdapter(mAdpter);
    }
}
